<template>
	<section style="width:100%">
		<section>
			<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
				<el-form :inline="true">
					<el-form-item>
						<el-input v-model="userId" placeholder="玩家ID"></el-input>
					</el-form-item>
					<el-form-item label="开始时间">
						<el-date-picker
								v-model="cashStart"
								type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss"
								placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
					<el-form-item label="结束时间">
						<el-date-picker
								v-model="cashEnd"
								type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss"
								placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" v-on:click="getUsers(pageSize)">查询</el-button>
					</el-form-item>
				</el-form>
			</el-col>
			<!--列表-->
			<el-table :data="users" stripe highlight-current-row v-loading="listLoading" style="width: 100%;" :default-sort = "{prop: 'time', order: 'descending'}">
				<el-table-column prop="id" label="#">
				</el-table-column>
				<el-table-column prop="number" label="订单号" width="150">
				</el-table-column>
				<el-table-column prop="userid" label="玩家ID" width="120" sortable>
				</el-table-column>
				<el-table-column prop="nickname" label="玩家昵称" width="120">
				</el-table-column>
				<el-table-column prop="commodityname" label="兑换实物" width="200">
				</el-table-column>
				<el-table-column prop="time" label="兑换时间" width="200" sortable>
				</el-table-column>
				<el-table-column prop="integralvalue" label="消耗奖励点" width="120">
				</el-table-column>
				<el-table-column prop="address" label="地址" width="250">
				</el-table-column>
				<el-table-column prop="realname" label="姓名" width="120">
				</el-table-column>
				<el-table-column prop="phone" label="电话" width="150">
				</el-table-column>
				<el-table-column prop="stateTxt" label="状态" width="100">
				</el-table-column>
				<el-table-column label="操作" min-width="200">
					<template slot-scope="scope" v-if="scope.row.state == 1">
						<!--<el-button type="primary" size="small" >处理完成</el-button>-->
						<el-button type="primary" size="small" @click="editCash(scope.row.id,'2')">发货</el-button>
						<el-button type="primary" size="small" @click="editCash(scope.row.id,'3')">拒绝发货</el-button>
					</template>
				</el-table-column>
			</el-table>

			<!--工具条-->
			<el-col :span="24" class="toolbar">
				<el-pagination layout="sizes, total, prev, pager, next" :page-sizes="[15, 30, 50]" @current-change="handleCurrentChange" @size-change="handleSizeChange"  :page-size="pageSize" :total="total" style="float:right;">
				</el-pagination>
			</el-col>

			<!--<el-dialog title="修改" v-model="editVisible" :close-on-click-modal="false">-->
				<!--<el-form :model="govern" label-width="130px" :rules="governRules" ref="govern">-->
					<!--<el-form-item label="状态选择">-->
						<!--<el-radio v-model="radio" label="1">未发货</el-radio>-->
						<!--<el-radio v-model="radio" label="2">已发货</el-radio>-->
						<!--<el-radio v-model="radio" label="3">拒绝</el-radio>-->
					<!--</el-form-item>-->
					<!--<el-form-item label="备注">-->
						<!--<el-input v-model="addForm.age" autosize type="textarea" width="100%"></el-input>-->
					<!--</el-form-item>-->
				<!--</el-form>-->
				<!--<div slot="footer" class="dialog-footer">-->
					<!--<el-button @click.native="editVisible = false">取消</el-button>-->
					<!--<el-button type="primary" @click.native="editSubmit" :loading="editLoading">确定</el-button>-->
				<!--</div>-->
			<!--</el-dialog>-->
		</section>
	</section>
</template>

<script>
	import base64 from '../../api/base64'

    export default {
        data() {
            return {
                userId:'',
                cashStart:'',
                cashEnd:'',
                listLoading:false,
                users:[],
                total:0,
                page:1,
				pageSize:15
            }
        },
        created(){
            let agentId = sessionStorage.getItem('searchAgentId')
            if (agentId){
                this.userId = agentId
                sessionStorage.removeItem('searchAgentId')
            }
        },
        methods: {
            handleSizeChange(val) {
                this.pageSize = val
                this.getUsers(this.pageSize);
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers(this.pageSize);
            },
            //获取用户列表
            getUsers(page) {
                this.listLoading = true;
                let loginStart,loginEnd
                if (this.cashStart || this.cashEnd){
                    loginStart = Date.parse(new Date(this.cashStart))
                    loginEnd = Date.parse(new Date(this.cashEnd))
                }else{
                    loginStart = ''
                    loginEnd =''
                }
                this.$axios.get('/integral-exchanges?userid='+this.userId+'&index='+this.page+'&size='+page+'&tradeatstart='+loginStart+'&tradeatend='+loginEnd,{},this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.total = res.data.count
                        let userTable = res.data.data
                        let base = new base64.Base64()
                        userTable.forEach((val)=>{
                            val.time = this.changeDate(val.submitat)
							if (val.nickname){
                                val.nickname = base.decode(val.nickname)
							}
                            val.realname =  base.decode(val.realname)
                            val.address =  base.decode(val.address)
							// val.integralvalue = parseFloat(val.integralvalue)/1000
                            val.phone =  base.decode(val.phone)
							if (val.realname == '0'){
                                val.realname = ''
							}
                            if (val.address == '0'){
                                val.address = ''
                            }
                            if (val.phone == '0'){
                                val.phone = ''
                            }
                            val.stateTxt = val.state.toString().replace('1','未发货').replace('2','已发货').replace('3','已拒绝')
                        })

                        this.users = userTable
                        this.listLoading = false
                    }else{
                        this.$message({message: res.data, type: 'error'});
                        this.listLoading = false
                    }
                })
            },
            editCash(id,state){
                let txt;
                if (state == '2'){
                    txt = '发货'
				}else{
                    txt = '拒绝发货'
				}
                this.$confirm('确认'+txt+'吗?', '提示', {
                    //type: 'warning'
                }).then(() => {
                    this.$axios.patch('/integral-exchange/'+id,{state:parseInt(state)},this.global().token).then((res)=>{
                        if (res.status == 200){
                            this.$message({message: '操作成功', type: 'success'});
                            this.getUsers(this.pageSize)
                        }else{
                            this.$message({message: res.data, type: 'error'});
                        }
                    })
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.cashStart = this.Todate1(new Date(new Date()-24*60*60*7000),'yyyy-MM-dd HH:mm:ss')
            this.cashEnd =  this.Todate(new Date(new Date()),'yyyy-MM-dd HH:mm:ss')
            this.getUsers(this.pageSize);
        }
    }

</script>

<style scoped lang="stylus">
	.agent-path
		display flex
		padding-bottom 10px
		&>p
			padding-right 10px
		&>h3
			cursor pointer
</style>